$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});


function sendFile(file, editor, $editable) {
    var data = new FormData();
    data.append("file", file);
    uploadurl = "/common/sysFile/upload";
    $.ajax({
        data: data,
        type: "POST",
        url: uploadurl,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            $('.summernote').summernote('insertImage', data.data);  //直接插入路径就行，filename可选
            console.log(data);
        },
        error:function(){
            alert("上传图片失败！");
        }
    });
}

$(document).ready(function() {
    $('.summernote').summernote({
        height: 400,
        minHeight: 300,
        maxHeight: 500,
        focus: true,
        lang:'zh-CN',
        placeholder: "文本输入区域",
        callbacks: {
            onImageUpload :function (files, editor, $editable) {
                sendFile(files[0], editor, $editable);
            }
        }
    });
});

function save() {
    var value = $(".summernote").summernote("code");
    $("#content").val(value);
	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/news/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}